package com.pr.makfilm.user.pages.room.create;

import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.domain.RoomPlace;
import com.pr.makfilm.domain.RoomPlaceTypeEnum;
import com.pr.makfilm.service.IRoomPlaceService;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.user.api.UserApiKeys;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.navigation.NavigateToSummaryRoomPage;
import com.pr.makfilm.user.pages.AbstractUserPage;
import com.pr.makfilm.user.pages.room.create.design.DesignCell;
import com.pr.makfilm.user.pages.room.create.design.DesignColumnHeader;
import com.pr.makfilm.user.pages.room.create.design.DesignRow;
import com.pr.makfilm.user.pages.room.create.design.DesignRowRepeater;
import com.pr.makfilm.user.pages.room.list.RoomListPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 30.09.13
 * Time: 23:28
 * To change this template use File | Settings | File Templates.
 */
@MountPath(UserApiMappings.DESIGN_ROOM_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class ConstructRoomPage extends AbstractUserPage
{
   @SpringBean
   private IRoomService roomService;

   @SpringBean
   private IRoomPlaceService roomPlaceService;

   private Room room;

   private List<RoomPlace> places = new ArrayList<RoomPlace>();

   public ConstructRoomPage(final PageParameters parameters)
   {
      super(parameters);
      room = getRoom(parameters);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      add(new DesignColumnHeader("topHeaders", new PropertyModel<Room>(this, "room")));
      add(new DesignColumnHeader("bottomHeaders", new PropertyModel<Room>(this, "room")));
      final DesignRowRepeater repeater = new DesignRowRepeater("rows", new PropertyModel<Room>(this, "room"));
      add(repeater);
      add(new AjaxLink<Void>("back")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            roomService.delete(room);
            setResponsePage(CreateRoomPage.class);
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("create.room.clean.confirm")));
         }
      });
      IndicatingAjaxLink<Void> submit = new IndicatingAjaxLink<Void>("submit")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            List<DesignRow> rows = repeater.getRows();
            for (DesignRow row : rows)
            {
               List<DesignCell> cells = row.getCells();
               for (DesignCell cell : cells)
               {
                  String css = cell.getCssClass();
                  if (StringUtils.contains(css, DesignCell.GREEN_CLASS))
                  {
                     savePlace(cell.getColumnId(), cell.getRowId(), RoomPlaceTypeEnum.FREE);

                  }
                  else if (StringUtils.contains(css, DesignCell.RED_CLASS))
                  {
                     savePlace(cell.getColumnId(), cell.getRowId(), RoomPlaceTypeEnum.BLOCKED);
                  }
               }
            }
            new NavigateToSummaryRoomPage(room).navigate();
         }
      };
      add(submit);
   }

   private RoomPlace savePlace(int column, int row, RoomPlaceTypeEnum type)
   {
      return roomPlaceService.create(room, column, row, type);
   }

   private Room getRoom(PageParameters parameters)
   {
      String sid = parameters.get(UserApiKeys.SID_KEY).toString();
      if (StringUtils.isNotBlank(sid))
      {
         Room obj = roomService.getBySid(sid);
         if (obj == null || obj.getObjectState() != ObjectStateEnum.INACTIVE)
         {
            throw new RestartResponseAtInterceptPageException(RoomListPage.class);
         }
         return obj;
      }
      else
      {
         throw new RestartResponseAtInterceptPageException(RoomListPage.class);
      }
   }
}
